package org.dubnation.base.util.verifycode.controller;

import org.dubnation.base.util.verifycode.service.IVerifyCodeService;
import org.dubnation.base.util.AjaxResult;
import org.dubnation.base.util.verifycode.dto.SendSmsCodeDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Pattern;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
     * 获取图片验证码
     * @return
     */
    @GetMapping("/imgCode/{uuid}")
    public AjaxResult getImgCode(@PathVariable("uuid") String uuid) {
        String imgCode = verifyCodeService.getImgCode(uuid);
        return AjaxResult.me().setData(imgCode);
    }

    /**
     * 获取手机验证码
     * @param sendSmsCodeDto
     * @return
     */
    @PostMapping("/phoneCode")
    public AjaxResult getPhoneCode(@RequestBody @Valid SendSmsCodeDto sendSmsCodeDto) {
        verifyCodeService.getPhoneCode(sendSmsCodeDto);
        return AjaxResult.me();
    }

    /**
     * 检查手机号码是否已注册过
     * @param phone
     * @return
     */
    @GetMapping("/checkPhone/{phone}")
    public AjaxResult checkPhone(@PathVariable("phone") @Pattern(regexp = "/^1[3456789]\\d{9}$/", message = "请输入正确的手机号码!") String phone) {
        verifyCodeService.checkPhone(phone);
        return AjaxResult.me();
    }
}
